<template>
  <div class="login-page">
    <div class="login-box">
      <h1>e店邦O2O平台</h1>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
            placeholder="用户名"
          >
          </el-input>
        </el-form-item>
        <el-form-item :label="'密码'" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            placeholder="密码"
            prefix-icon="el-icon-lock"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="captchacode">
          <div class="captcha-box">
            <el-input
              v-model.number="ruleForm.captchacode"
              placeholder="验证码"
            >
            </el-input>
            <img height="40" :src="captchaSrc" alt="" @click="getCaptchaCode" />
          </div>
        </el-form-item>
        <el-form-item id="login-btn-box">
          <el-button
            class="login-btn"
            type="primary"
            @click="submitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="login-footer"><span>Copyright © 2018-2022 ruoyi.vip All Rights Reserved.</span></div>
  </div>
</template>

<script>
import { validateUsername } from "@/utils/vaildate";
import { getCaptchaCodeApi, LoginApi } from "@/API/yjl-Api/index";
import { mapMutations, mapActions } from "vuex";

export default {
  data() {
    return {
      ruleForm: {
        username: "hc",
      
        password: "123456",
               captchacode: "888888",
      },
      rules: {
        username: [
          {
            required: true,
            message: "用户名不能为空",
            trigger: "blur",
          },
          {
            // 自定义校验规则
            validator: validateUsername,
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
        ],
        captchacode: [
          {
            required: true,
            message: "验证码不能为空",
            trigger: "blur",
          },
        ],
      },
      captchaSrc: "",
    };
  },
  methods: {
    ...mapActions("userinfo", ["asyncGetUserInfo"]),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 校验通过
          let res = await LoginApi({
            username: this.ruleForm.username,
            password: this.ruleForm.password,
            code: this.ruleForm.captchacode,
            uuid: localStorage.getItem("edb-captcha-uuid"),
          });
          // 返回状态码不是 200 return出去
          if (res.code !== 200) {
            this.$message({
              message: res.msg,
              type: "error",
              duration: 1000,
            });
            return;
          } else {
            // 提示用户登录成功
            this.$message({
              message: "登录成功",
              type: "success",
              duration: 1000,
            });
            // 清除uuid
            localStorage.removeItem("edb-captcha-uuid");
            // 存储token
            localStorage.setItem("Authorization", res.token);
            // 用户信息
            this.asyncGetUserInfo();
            // 跳转
            this.$router.push("/");
          }
        } else {
          // 校验不通过
          this.$message({
            message: "请输入正确的用户名和密码",
            type: "warning",
          });
          return false;
        }
      });
    },
    async getCaptchaCode() {
      let res = await getCaptchaCodeApi();
      if (!res) return;
      // 展示验证码图片
      this.captchaSrc = "data:imgage/gif;base64," + res.img;
      // 保存 uuid 登录的时候作为参数传给后端
      localStorage.setItem("edb-captcha-uuid", res.uuid);
    },
    ...mapMutations("userMenuData", ["changeMenuData"]),
  },
  created() {
    this.getCaptchaCode();
    this.changeMenuData([]); // 清空vuex中得数据
  },
};
</script>

<style lang="less" scoped>
.login-page {
  width: 100%;
  height: 100%;
  background: url("http://xue.cnkdl.cn:23683/static/img/login-background.f9f49138.jpg") center no-repeat;
  background-size: cover;
  position: relative;
  .login-box {
    width: 400px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding-top: 20px;
    padding-right: 55px;
    border-radius: 10px;
    box-shadow: 0 0 10px #000;
    h1 {
      text-align: center;
      margin-bottom: 20px;
      padding-left: 55px;
      font-size: 20px;
    }
    .captcha-box {
      display: flex;
      img {
        margin-left: 40px;
      }
    }
    .login-btn {
      width: 100%;
    }

    ::v-deep #login-btn-box .el-form-item__content {
      margin-left: 40px !important;
    }
  }
  .login-footer{
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    right: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}
</style>
